﻿@{
    Layout = "";

    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;
}
@model ShippingByWeightListModel
@using Nop.Plugin.Shipping.ByWeight.Models;
@using Nop.Web.Framework;
@using Nop.Core.Infrastructure;

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "shipping-byweight-form" }))
{
    @Html.AntiForgeryToken()

    <div class="content-header clearfix">
        <div class="pull-right">
            <a href="@Url.Action("Methods", "Shipping", new {area = "Admin"})" class="btn bg-blue">
                <i class="fa fa-truck"></i>
                @T("Admin.Configuration.Shipping.Methods.Manage")
            </a>
            <a href="@Url.Action("Restrictions", "Shipping", new {area = "Admin"})" class="btn bg-blue">
                <i class="fa fa-globe"></i>
                @T("Admin.Configuration.Shipping.Restrictions.Manage")
            </a>
        </div>
    </div>

    <div class="panel-group">
        <div class="panel panel-default">
            <div class="panel-body">
                <p>
                    <b>@T("Plugins.Shipping.ByWeight.Formula")</b> 
                    @T("Plugins.Shipping.ByWeight.Formula.Value")
                </p>
                <div id="shipping-byweight-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#shipping-byweight-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("RatesList", "ShippingByWeight", new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Shipping.ByWeight.Controllers" }, { "area", "" } }))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    },
                                    destroy: {
                                        url: "@Html.Raw(Url.Action("RateDelete", "ShippingByWeight", new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Shipping.ByWeight.Controllers" }, { "area", "" } }))",
                                        type: "POST",
                                        dataType: "json",
                                        data: addAntiForgeryToken
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors",
                                    model: {
                                        id: "Id"
                                    }
                                },
                                requestEnd: function (e) {
                                    if (e.type == "update") {
                                        this.read();
                                    }
                                },
                                error: function (e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                pageSize: @(defaultGridPageSize),
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                pageSizes: [@(gridPageSizes)]
                            },
                            editable: {
                                confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "StoreName",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.Store")",
                                width: 200
                            }, {
                                field: "WarehouseName",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.Warehouse")",
                                width: 200
                            }, {
                                field: "CountryName",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.Country")",
                                width: 200
                            }, {
                                field: "StateProvinceName",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.StateProvince")",
                                width: 200
                            }, {
                                field: "Zip",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.Zip")",
                                width: 200
                            }, {
                                field: "ShippingMethodName",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.ShippingMethod")",
                                width: 200
                            }, {
                                field: "DataHtml",
                                title: "@T("Plugins.Shipping.ByWeight.Fields.DataHtml")",
                                width: 400,
                                encoded: false
                            }, {
                                field: "Id",
                                title: "@T("Admin.Common.Edit")",
                                width: 100,
                                template: "<input type='submit' value='@T("Admin.Common.Edit")' onclick=\"javascript:OpenWindow('@Url.RouteUrl("Plugin.Shipping.ByWeight.EditPopup")?id=#=Id#&btnId=btnRefresh&formId=shipping-byweight-form', 800, 800, true); return false;\" class='btn btn-default' />"
                            },  {
                                command: { name: "destroy", text: "@T("Admin.Common.Delete")" },
                                title: "@T("Admin.Common.Delete")",
                                width: 100
                            }]
                        });
                    });
                </script>
            </div>
            <div class="panel-footer">
                @{
                    var addNewUrl = Url.RouteUrl("Plugin.Shipping.ByWeight.AddPopup") + "?btnId=btnRefresh&formId=shipping-byweight-form";
                }
                <input type="submit" id="btnAddNewOption" class="btn btn-primary" value="@T("Plugins.Shipping.ByWeight.AddRecord")" onclick="javascript:OpenWindow('@(addNewUrl)', 800, 800, true); return false;"/>
                <input type="submit" id="btnRefresh" class="btn btn-default" style="display: none" />
            </div>
                    
            <script type="text/javascript">
                $(document).ready(function () {
                    $('#btnRefresh').click(function () {
                        //refresh grid
                        var grid = $("#shipping-byweight-grid").data('kendoGrid');
                        //grid.dataSource.page(1); //new search. Set page size to 1
                        grid.dataSource.read();

                        //return false to don't reload a page
                        return false;
                    });
                });
            </script>
        </div>
        <div class="panel panel-default">
            <div class="panel-body">
                <div class="form-group">
                    <div class="col-md-3">
                        @Html.NopLabelFor(model => model.LimitMethodsToCreated)
                    </div>
                    <div class="col-md-9">
                        @Html.NopEditorFor(model => model.LimitMethodsToCreated)
                        @Html.ValidationMessageFor(model => model.LimitMethodsToCreated)
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-3">
                        &nbsp;
                    </div>
                    <div class="col-md-9">
                        <input type="button" id="savegeneralsettings" class="btn btn-primary" value="@T("Admin.Common.Save")" />

                        <script type="text/javascript">
                            $(document).ready(function () {
                                $('#savegeneralsettings').click(function () {

                                    var postData = $(this.form).serialize();
                                    addAntiForgeryToken(postData);

                                    $.ajax({
                                        cache: false,
                                        type: 'POST',
                                        url: '@Url.RouteUrl("Plugin.Shipping.ByWeight.SaveGeneralSettings")',
                                        data: postData,
                                        dataType: 'json',
                                        success: function (data) {
                                            alert('Saved');
                                        },
                                        error: function (xhr, ajaxOptions, thrownError) {
                                            alert('Failed to add record.');
                                        }
                                    });
                                    return false;
                                });
                            });
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
}
